package com.xxx.servlet;

import com.alibaba.fastjson.JSON;
import com.xxx.pojo.Employee;
import com.xxx.pojo.PageTools;
import com.xxx.service.Services;
import com.xxx.service.impl.EmployeeServiceImpl;
import com.xxx.util.GetIdTime;
import com.xxx.util.JsonSwitchUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/employee/*")
public class EmployeeServlet extends BaseServlet {

    private Services service = new EmployeeServiceImpl();


    private Long id = GetIdTime.getId();
    public void selectByUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Employee employee =(Employee)service.selectByUsername(username);


        if (employee != null && password.equals(employee.getPassword())){
            System.out.println("zhuanfa");
            resp.sendRedirect("/LS02_Impl/login/index.html");
        }else {
            JsonSwitchUtil.toJsonString(req,resp,"false");
        }

    }

    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = req.getReader();
        String readLine = bufferedReader.readLine();
        System.out.println(readLine);
        PageTools pageTools = JSON.parseObject(readLine, PageTools.class);
        List<Employee> employees = service.selectAll((pageTools.getPageNo()-1)* pageTools.getPageSize(), pageTools.getPageSize());
        System.out.printf("selectAll:" + employees);
        JsonSwitchUtil.toJsonString(req,resp,employees);

    }

    public void addEmployee(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        BufferedReader br = req.getReader();
        String line = br.readLine();
        Employee employee = JSON.parseObject(line, Employee.class);
        Long id = Long.valueOf(GetIdTime.getId() + "2");
//        String name = req.getParameter("name");
//        String username = req.getParameter("username");
//        String password = req.getParameter("password");
//        String phone = req.getParameter("phone");
//        String sex = req.getParameter("sex");
//        String idNumber = req.getParameter("idNumber");
//        Integer status = Integer.valueOf(req.getParameter("status"));
        String createTime = GetIdTime.getDataTime();
        Long createUser = Long.valueOf("12345678");
        employee.setId(id);
        employee.setCreateTime(createTime);
        employee.setUpdateTime(createTime);
        employee.setCreateUser(createUser);
        employee.setUpdateUser(createUser);
        System.out.println("添加：" + employee);

        int i = service.add(employee);
        JsonSwitchUtil.toJsonString(req,resp,i);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        BufferedReader reader = req.getReader();
        String line = reader.readLine();

        Long id = JSON.parseObject(line, Long.class);
        resp.setContentType("application/jsom;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();

        int delete = service.delete(id);
        String json = JSON.toJSONString(delete);
        writer.write(json);


    }

    public void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        Long id = Long.valueOf(req.getParameter("id"));

        Employee employee = (Employee)service.selectById(id);

        req.setAttribute("employee", employee);

        selectAll(req, resp);
    }


    public void updateById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        req.setCharacterEncoding("UTF-8");

//        Long id = Long.valueOf(req.getParameter("id"));
//        String name = req.getParameter("name");
//        String username = req.getParameter("username");
//        String password = req.getParameter("password");
//        String phone = req.getParameter("phone");
//        String sex = req.getParameter("sex");
//        String idNumber = req.getParameter("idNumber");
//        Integer status = Integer.valueOf(req.getParameter("status"));
//
//        Employee employee = new Employee();
//
//        employee.setId(id);
//        employee.setName(name);
//        employee.setPassword(password);
//        employee.setUsername(username);
//        employee.setPhone(phone);
//        employee.setSex(sex);
//        employee.setIdNumber(idNumber);
//        employee.setStatus(status);
//        employee.setUpdateTime(GetIdTime.getDataTime());
//        employee.setUpdateUser(this.id);

        BufferedReader br = req.getReader();
        String readLine = br.readLine();
        Employee employee = JSON.parseObject(readLine, Employee.class);

        int i = service.updateById(employee);
        String json = JSON.toJSONString(i);
        resp.setContentType("application/json;utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(json);
    }

    public void selectCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int i = service.selectCount();
        JsonSwitchUtil.toJsonString(req,resp,i);
    }
}
